package com.xin.data.simbapesa.fragment;

import static android.app.Activity.RESULT_OK;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.IntentSenderRequest;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.viewbinding.ViewBinding;

import com.xin.data.simbapesa.R;
import com.xin.data.simbapesa.SimbaApp;
import com.xin.data.simbapesa.activity.SimbaCustomerServiceActivity;
import com.xin.data.simbapesa.activity.SimbaExitActivity;
import com.xin.data.simbapesa.activity.SimbaFeedbackActivity;
import com.xin.data.simbapesa.adapter.SettingAdapter;
import com.xin.data.simbapesa.api.PU;
import com.xin.data.simbapesa.api.U;
import com.xin.data.simbapesa.base.BaseActivity;
import com.xin.data.simbapesa.base.BaseBindingAdapter1;
import com.xin.data.simbapesa.base.BaseFragment;
import com.xin.data.simbapesa.databinding.SimbaAccountSubmitBinding;
import com.xin.data.simbapesa.databinding.SimbaSettingFragmentBinding;
import com.xin.data.simbapesa.device.ImmediateUpdateManager;
import com.xin.data.simbapesa.obj.SimbaUserProcess;
import com.xin.data.simbapesa.utils.DateUtils;
import com.xin.data.simbapesa.utils.LogUtil;
import com.xin.data.simbapesa.utils.ResUtils;
import com.xin.data.simbapesa.utils.SpUtils;
import com.xin.data.simbapesa.utils.ViewUtils;
import com.xin.data.simbapesa.view.SimbaSpaceItemDecoration;

import java.util.ArrayList;
import java.util.List;

import kotlin.Unit;
import kotlin.jvm.functions.Function0;

public class SettingFragment extends BaseFragment<SimbaSettingFragmentBinding> {


    SettingAdapter adapter;
    List<String> list;

    BaseActivity<ViewBinding> mActivity;

    StateFragment2.OnRefreshState2Listener onRefreshStateListener;

    ImmediateUpdateManager updateManager = null;


    @Override
    public void onAttach(@androidx.annotation.NonNull Context context) {
        super.onAttach(context);
        try {
            onRefreshStateListener = (StateFragment2.OnRefreshState2Listener) context;
        } catch (ClassCastException ignored) {
        }
    }


    @Override
    protected SimbaSettingFragmentBinding initViewBinding(LayoutInflater inflater, ViewGroup container) {
        return SimbaSettingFragmentBinding.inflate(inflater);
    }

    @Override
    protected void initView() {


        mActivity = (BaseActivity<ViewBinding>) getActivity();

        binding.getRoot().setPadding(0, 0, 0, 0);

        mActivity.setStatusBarHeight(binding.tvRoot);


        list = new ArrayList<>();
        list.add(ResUtils.s(R.string.setting_title1));
        list.add(ResUtils.s(R.string.setting_title2));
        list.add(ResUtils.s(R.string.setting_title3));


        LinearLayoutManager manager1 = new LinearLayoutManager(getActivity());
        manager1.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerView.setLayoutManager(manager1);
        int d = ResUtils.d(R.dimen.dp_6);
        binding.recyclerView.addItemDecoration(new SimbaSpaceItemDecoration(d, d, getActivity()));
        adapter = new SettingAdapter(list);
        adapter.setOnBtnItemClickListener(new BaseBindingAdapter1.BtnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (position == 0) {
                    startActivity(new Intent(getActivity(), SimbaFeedbackActivity.class));
                } else if (position == 1) {
                    PU.userBuriedPoint(PU.P1631, "");
                    if (updateManager != null) {
                        updateManager.checkAndStartUpdateAsync(new Function0<Unit>() {
                            @Override
                            public Unit invoke() {
                                Toast.makeText(getActivity(), ResUtils.s(R.string.update_hit), Toast.LENGTH_LONG).show();
                                return null;
                            }
                        }, null);
                    } else {
                        Toast.makeText(getActivity(), ResUtils.s(R.string.update_hit), Toast.LENGTH_LONG).show();
                    }
                } else if (position == 2) {
                    startActivity(new Intent(getActivity(), SimbaExitActivity.class));
                }
            }
        });
        binding.recyclerView.setAdapter(adapter);

        binding.tvCs.setOnClickListener(v -> {
            ViewUtils.viewScale(v);
            PU.userBuriedPoint(PU.P23, "");
            startActivity(new Intent(getActivity(), SimbaCustomerServiceActivity.class));
        });

        initUpdate();

    }


    void initUpdate() {
        try {
            updateManager = new ImmediateUpdateManager(mActivity, true, updateLauncher, null);
        } catch (Exception e) {
        }
    }

    final ActivityResultLauncher<IntentSenderRequest> updateLauncher =
            registerForActivityResult(new ActivityResultContracts.StartIntentSenderForResult(),
                    result -> {
                        try {
                            if (result.getResultCode() == RESULT_OK) {
                                SpUtils.save_boolean(SimbaApp.getInstance(), SpUtils.KEY_UPDATE + "_" + U.URL_PHONE + "_" + DateUtils.getYear() + "_" + DateUtils.getMonth() + "_" + DateUtils.getDay(), true);
                            }
                        } catch (Exception e) {
                            LogUtil.e("me", "updateLauncher e->" + e);
                        }
                    });

    @Override
    protected void initData() {
        UP_TYPE1 = PU.P18;
        UP_TYPE2 = PU.P19;
    }

    @Override
    public void onResume() {
        super.onResume();
        setUserData();
    }

    void setUserData() {
        if (getActivity() != null) {
            binding.tvSubTitle.setText(SpUtils.get_string(getActivity(), SpUtils.KEY_PHONE, ""));
            String name = "";
            SimbaUserProcess userProcess = SimbaApp.getInstance().getProcess();
            if (userProcess != null && !TextUtils.isEmpty(userProcess.getFullName())) {
                name = userProcess.getFullName();
            }
            if (!TextUtils.isEmpty(name)) {
                binding.tvTitle.setText(name);
            }
        }
    }


}
